package com.heima.shengchanzhe;

import java.util.concurrent.locks.ReentrantLock;

public class MyLock implements Runnable {
    private int ticket = 100;
    //private Object lock = new Object();
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {

        try {
            lock.lock();
        } catch (Exception e) {
            e.printStackTrace();
        }
        while (true) {
            // synchronized (lock){
            if (ticket <= 0) {
                break;
            } else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ticket--;
                System.out.println(Thread.currentThread().getName() + "在买票剩" + ticket + "张");
                lock.unlock();
            }
        }
    }
}